//
//  GHLoginViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BBPhoneLoginViewController.h"
#import "UITextField+STInputLimit.h"
#import "TMForgotViewController.h"
#import "TMRegisterViewController.h"
#import "TMTabbarViewController.h"
#define textFiledBackgroundColor [UIColor colorWithRed:77/255.0 green:96/255.0 blue:129/255.0 alpha:1]
@interface BBPhoneLoginViewController ()
@property(nonatomic, strong) UITextField                     *accountTextFiled;
@property(nonatomic, strong) UITextField                     *pwdTextFiled;
@end

@implementation BBPhoneLoginViewController
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"手机号登录";
    [self configSubView];
    [self addNotifacations];
    self.view.backgroundColor = [BBThemeColorManger manger].backGroundColor;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
#pragma mark --Notifacation
- (void)addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldTextDidChangeNotification:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    
}
- (void)textFieldTextDidChangeNotification:(NSNotification*)notify{
    if (notify.object == self.pwdTextFiled) {
        [self.pwdTextFiled st_textInputLimitWithLength:16];
    }else if (notify.object == self.accountTextFiled){
        [self.accountTextFiled st_textInputLimitWithLength:11];
    }
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark --subView
- (void)configSubView{
    //    [UIColor colorWithRed:81.0/255.0 green:136/255.0 blue:210/255.0 alpha:1];
    //     [UIColor colorWithRed:77/255.0 green:96/255.0 blue:129/255.0 alpha:1];

    
    
    UIView * accountView = [self configAccountTextFiled];
    accountView.st_top = [TMUtils navgationBarBootom] + 20;
    NSString * account = [[NSUserDefaults standardUserDefaults] objectForKey:loginAccountKey];
    self.accountTextFiled.text = account;
    [self.view addSubview:accountView];
    
    UIView * pwdView = [self configpwdTextFiled];
    pwdView.st_top = accountView.st_bottom + 20;
    NSString * pwd = [[NSUserDefaults standardUserDefaults] objectForKey:loginAccountPwdKey];
    self.pwdTextFiled.text = pwd;
    [self.view addSubview:pwdView];
    
    __weak typeof(self) weakSelf =  self;

    
    STButton * forgotButton = [[STButton alloc] initWithFrame:CGRectMake(30 + 22, pwdView.st_bottom + 10, 100, 44)
                                                        title:@"忘记密码?"
                                                   titleColor:TM_ThemeBackGroundColor
                                                    titleFont:13
                                                 cornerRadius:0
                                              backgroundColor:[UIColor clearColor]
                                              backgroundImage:nil
                                                        image:nil];
    forgotButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [forgotButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedforgotButton];
    }];
    forgotButton.left = pwdView.left;
    [self.view addSubview:forgotButton];
    
    STButton * registerButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0 + 20, 100, 44)
                                                          title:@"立即注册"
                                                     titleColor:TM_ThemeBackGroundColor
                                                      titleFont:13
                                                   cornerRadius:0
                                                backgroundColor:[UIColor clearColor]
                                                backgroundImage:nil
                                                          image:nil];
    registerButton.st_right = pwdView.st_right;
    registerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [registerButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedregisterButton];
    }];
    registerButton.centerY = forgotButton.centerY;
    [self.view addSubview:registerButton];
    


    STButton * loginButton = [[STButton alloc] initWithFrame:CGRectMake(30, forgotButton.st_bottom + 30, UIScreenWidth - 60, 50)
                                                       title:@"登录"
                                                  titleColor:[UIColor whiteColor]
                                                   titleFont:18
                                                cornerRadius:4
                                             backgroundColor:TM_ThemeBackGroundColor
                                             backgroundImage:nil
                                                       image:nil];
    loginButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [loginButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedLoginButton];
    }];
    [self.view addSubview:loginButton];
    
    if (BBThemeColorManger.isBlack) {
        [registerButton setTitleColor:SecendTextColor forState:UIControlStateNormal];
        [forgotButton setTitleColor:SecendTextColor forState:UIControlStateNormal];
        pwdView.backgroundColor = BBThemeColorManger.blackStyletableGrayBackColor;
        self.accountTextFiled.backgroundColor =BBThemeColorManger.blackStyletableGrayBackColor;
        self.pwdTextFiled.backgroundColor =BBThemeColorManger.blackStyletableGrayBackColor;
        self.accountTextFiled.textColor = [UIColor whiteColor];
        self.pwdTextFiled.textColor = [UIColor whiteColor];
        self.pwdTextFiled.attributedPlaceholder = [self.pwdTextFiled.placeholder st_convertAttributeStringWithKeyWord:self.pwdTextFiled.placeholder attributes:@{NSForegroundColorAttributeName:UIColor.whiteColor}];
        self.accountTextFiled.attributedPlaceholder = [self.accountTextFiled.placeholder st_convertAttributeStringWithKeyWord:self.accountTextFiled.placeholder attributes:@{NSForegroundColorAttributeName:UIColor.whiteColor}];
        accountView.backgroundColor = BBThemeColorManger.blackStyletableGrayBackColor;
        loginButton.backgroundColor = BBThemeColorManger.blackStyletableGrayBackColor;
    }
}
- (UIView*)configAccountTextFiled{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)];
    view.backgroundColor = [UIColor whiteColor];
   // view.layer.cornerRadius = 22;
  //  view.clipsToBounds = YES;
    UITextField * textFiled = [[UITextField alloc] initWithFrame:CGRectMake(22, 0, view.st_width - 44, 44)];
    textFiled.placeholder = @"用户名/手机号";
    textFiled.keyboardType = UIKeyboardTypeNumberPad;
    UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0 ,25, 44)];
    UIImageView * leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 3, 14, 18)];
    leftImageView.centerY = leftView.height / 2;
    leftImageView.image = [UIImage imageNamed:@"手机3"];
    [leftView addSubview:leftImageView];
    textFiled.leftView = leftView;
    textFiled.leftViewMode = UITextFieldViewModeAlways;
    textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    textFiled.backgroundColor =  [UIColor whiteColor];;
    [view addSubview:textFiled];
    self.accountTextFiled = textFiled;
    return view;
}
- (UIView*)configpwdTextFiled{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)];
    view.backgroundColor = [UIColor whiteColor];
  //  view.layer.cornerRadius = 22;
 //   view.clipsToBounds = YES;
    UITextField * textFiled = [[UITextField alloc] initWithFrame:CGRectMake(22, 0, view.st_width - 44, 44)];
    textFiled.placeholder = @"密码";
    UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 23, 44)];
    UIImageView * leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 3, 12, 15)];
    leftImageView.image = [UIImage imageNamed:@"密码3"];
    leftImageView.centerY = leftView.height / 2;
    [leftView addSubview:leftImageView];
    textFiled.leftView = leftView;
    textFiled.backgroundColor = [UIColor whiteColor];;
    textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    textFiled.keyboardType = UIKeyboardTypeASCIICapable;
    textFiled.leftViewMode = UITextFieldViewModeAlways;
    [view addSubview:textFiled];
    textFiled.secureTextEntry = YES;
    self.pwdTextFiled = textFiled;
    return view;
}
#pragma mark --Action Method
- (void)onSelectedLoginButton{
    if (self.accountTextFiled.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    if (self.accountTextFiled.text.length != 11) {
        [SVProgressHUD showErrorWithStatus:@"手机号格式错误"];
        return;
    }
    if (self.pwdTextFiled.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    [SVProgressHUD showWithStatus:@"登录中"];
    [self loginWithMobie:self.accountTextFiled.text pwssWord:self.pwdTextFiled.text];
}
- (void)onSelectedforgotButton{
    [self.navigationController pushViewController:[TMForgotViewController new] animated:YES];
}
- (void)onSelectedregisterButton{
    
    [self.navigationController pushViewController:[TMRegisterViewController new] animated:YES];
    //[self.navigationController.view st_showAnimationWithType:STAnimationTypekCATransitionFade];
}
- (void)onSelectedQQButton{
    
}
- (void)onSelectedWeChatButton{
    
}
- (void)onSelectedSinaButton{
    
}
#pragma mark --NetWork Method
- (void)loginWithMobie:(NSString *)mobile pwssWord:(NSString *)passWord{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if (mobile.length) {
        [dic setObject:mobile forKey:@"phone"];
    }else{
        NSLog(@"登录手机号为空");
        return;
    }
    if (passWord.length) {
        [dic setObject:passWord forKey:@"password"];
    }else{
        NSLog(@"登录密码为空");
        return;
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",severHeader,@"/api/user/login"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypeGet
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"登录成功"];
                                                 NSDictionary * modelDIc = [responseObject valueForKey:@"user"];
                                                 BBUserModel * user = [BBUserModel mj_objectWithKeyValues:modelDIc];
                                                 user.password = passWord;
                                                 [[STUserManger defult] updateUserModel:user];
                                                 
                                                 [[NSUserDefaults standardUserDefaults] setObject:mobile forKey:loginAccountKey];
                                                 [[NSUserDefaults standardUserDefaults] setObject:passWord forKey:loginAccountPwdKey];
                                                 [[NSUserDefaults standardUserDefaults] synchronize];
                                                 
                                                 [self.view endEditing:YES];
                                                 [self.navigationController popToRootViewControllerAnimated:YES];
                                                 [[UIApplication sharedApplication].keyWindow st_showAnimationWithType:STAnimationTyperippleEffect duration:2];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}

@end


